/// @tags:
#include <cassert>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#include <unordered_map>

using std::cin, std::cout, std::endl;

namespace BlueQuantum {

constexpr int maxn = 2e5, maxblk = 640;

int a[maxn + 1], b[maxn + 1];

std::unordered_map<int, int> map[maxblk + 1];

int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int blk = sqrt(2 * n);
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
    }
    int max = 0;
    for (int i = 1; i <= n; ++i) {
      cin >> b[i];
      max = std::max(max, b[i]);
      if (a[i] <= blk) {
        map[a[i]][b[i]]++;
      }
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (a[i] > blk) {
        for (int j = 1; j <= blk && a[i] * j <= n * 2; ++j) {
          if (a[i] * j - b[i] >= 1) {
            auto iter = map[j].find(a[i] * j - b[i]);
            if (iter != map[j].end()) ans += iter->second;
          }
        }
      }
    }
    long long tmp = 0;
    for (int i = 1; i <= blk; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (i * a[j] <= n * 2 && (i * a[j] - b[j] >= 1)) {
          auto iter = map[i].find(i * a[j] - b[j]);
          if (iter != map[i].end()) tmp += iter->second;
          if (a[j] == i && (a[j] * i == b[j] + b[j])) tmp--;
        }
      }
    }
    ans += tmp;
    cout << ans / 2 << '\n';
    for (int i = 1; i <= blk; ++i) {
      map[i].clear();
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("D. The BOSS Can Count Pairs.in", "r", stdin);
  freopen("D. The BOSS Can Count Pairs.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
